package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.ManyThread.TotalScore;
import com.tfjybj.itoo.exam.entity.JudgeTaskEntity;
import com.tfjybj.itoo.exam.model.*;
import com.tfjybj.itoo.exam.provider.service.JudgeTaskService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * JudgeTaskController
 * judgeTask表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"judgeTask表接口"})
@RequestMapping(value = "/judgeTask")
@RestController
@Slf4j
public class JudgeTaskController {

    @Resource
    private JudgeTaskService judgeTaskService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model JudgeTaskModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody JudgeTaskModel model) {
        if (StringUtils.isEmpty(model.getExaminationId())) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        JudgeTaskEntity judgeTaskEntity = new JudgeTaskEntity();
        BeanUtils.copyProperties(model, judgeTaskEntity);
        judgeTaskService.save(judgeTaskEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        judgeTaskService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        judgeTaskService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model JudgeTaskModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改judgeTask")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody JudgeTaskModel model) {
        if (StringUtils.isEmpty(model.getExaminationId())) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        JudgeTaskEntity judgeTaskEntity = new JudgeTaskEntity();
        BeanUtils.copyProperties(model, judgeTaskEntity);
        judgeTaskService.updateById(judgeTaskEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找JudgeTask
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        JudgeTaskEntity judgeTaskEntity = judgeTaskService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskEntity);
    }

    /**
     * 分页查询所有JudgeTask
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有JudgeTask")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<JudgeTaskEntity> judgeTasks = judgeTaskService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTasks);
    }

    /**
     * 根据批卷班级名称模糊查询judgeTask
     *
     * @param teachclassName 批卷班级名称
     * @param pageNo         页码
     * @param pageSize       页数
     * @return 模糊查询的judgeTask
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据批卷班级名称模糊查询judgeTask", notes = "分页根据批卷班级名称模糊查询judgeTask")
    @GetMapping(value = "queryJudgeTaskByLikeTeachclassName/{pageNo}/{pageSize}")
    public ItooResult queryJudgeTaskByLikeTeachclassName(@RequestParam(required = false, defaultValue = "") String teachclassName,
                                                         @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                         @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<JudgeTaskEntity> judgeTaskList = judgeTaskService.queryByLikeTeachclassName(teachclassName, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */


    /**
     * 根据实体内容进行查询
     *
     * @param model
     * @return * @author 侯旭日
     * @since 2.0.0 2018-11-16 19:48:26
     */
    @ApiOperation(value = "根据实体内容进行查询")
    @GetMapping(value = {"/queryAllByModel"})
    public ItooResult queryAllByModel(@ModelAttribute JudgeTaskModelNorequired model) {
        List<JudgeTaskModelResult> judgeTaskModelNorequireds = judgeTaskService.queryAllByModel(model);
        //如果通过人员ID拿到人员姓名需要再调用接口
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskModelNorequireds);
    }

    /**
     * 根据model内容进行查询
     *
     * @param model
     * @return * @author 刘梦娇
     * @since 2.0.0 2018年12月13日20:18:44
     */
    @ApiOperation(value = "根据判分任务表内容进行查询")
    @GetMapping(value = {"/queryAllByJudgeTask"})
    public ItooResult queryAllByJudgeTask(@ModelAttribute JudgeTaskModelNorequired model) {
        List<JudgeTaskModelResult> judgeTaskModelNorequireds = judgeTaskService.queryAllByJudgeTask(model);
        //如果通过人员ID拿到人员姓名需要再调用接口
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskModelNorequireds);
    }

    /**
     * 绑定试卷名称下拉
     *
     * @return * @author 侯旭日
     * @since 2.0.0 2018-11-18 19:48:26
     */
    @ApiOperation(value = "绑定试卷名称下拉")
    @GetMapping(value = {"/queryDistinctExam"})
    public ItooResult queryDistinctExam() {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskService.queryDistinctExam());
    }

    /**
     * 绑定大题号下拉
     *
     * @return * @author 侯旭日
     * @since 2.0.0 2018-11-18 19:48:26
     */
    @ApiOperation(value = "绑定大题号下拉")
    @GetMapping(value = {"/queryDistinctBigQues"})
    public ItooResult queryDistinctBigQues() {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskService.queryDistinctBigQues());
    }

    /**
     * 绑定班级下拉
     *
     * @return
     * @author 侯旭日
     * @since 2.0.0 2018-11-18 19:48:26
     */
    @ApiOperation(value = "绑定班级下拉")
    @GetMapping(value = {"/queryDistinctClass"})
    public ItooResult queryDistinctClass() {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskService.queryDistinctClass());
    }

    /**
     * 根据考试Id、题型Id、班级Id查询所有未判分总数
     *
     * @param examId
     * @param questionTypeId
     * @param examClassId
     * @return
     * @author 侯旭日
     * @since 2.0.0 2018-11-15 19:48:26
     */
    @ApiOperation(value = "根据考试Id、题型Id、班级Id查询所有未判分总数", notes = "根据考试Id、题型Id、班级Id查询所有未判分总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试Id", dataType = "string", required = true),
            @ApiImplicitParam(name = "questionTypeId", value = "题型Id", dataType = "string", required = true),
            @ApiImplicitParam(name = "examClassId", value = "班级Id", dataType = "string", required = true)
    })
    @GetMapping(value = "queryJudgeSituation/{examId}/{questionTypeId}/{examClassId}")
    public ItooResult queryJudgeSituation(@PathVariable String examId, @PathVariable String questionTypeId, @PathVariable String examClassId) {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskService.queryJudgeSituation(examId, questionTypeId, examClassId));
    }

    /**
     * 根据考试Id、题型Id、班级Id查询所有答题记录
     *
     * @param examId         考试Id
     * @param questionTypeId 题型Id
     * @param examClassId    班级Id
     * @return
     * @author 侯旭日
     * @since 2.0.0 2018-11-23 19:48:2
     */
    @ApiOperation(value = "根据考试Id、题型Id、班级Id查询所有答题记录", notes = "根据考试Id、题型Id、班级Id查询所有答题记录")
    @GetMapping(value = "queryJudgeQues/{examId}/{questionTypeId}/{examClassId}")
    public ItooResult queryJudgeQues(
            @ApiParam(name = "examId", value = "考试Id", required = true) @PathVariable String examId,
            @ApiParam(name = "questionTypeId", value = "题型Id", required = true) @PathVariable String questionTypeId,
            @ApiParam(name = "examClassId", value = "班级Id", required = true) @PathVariable String examClassId) {
        List<PaperRecordModel> paperRecordModels = judgeTaskService.queryJudgeQues(examId, questionTypeId, examClassId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", paperRecordModels);
    }

    /**
     * 更新每小题分数
     *
     * @param id
     * @param score
     * @return
     * @author 侯旭日
     * @since 2.0.0 2018-11-23 19:48:2
     */
    @ApiOperation(value = "更新每小题分数")
    @PostMapping(value = {"/updateManualJudge/{id}/{score}/"})
    public ItooResult updateManualJudge(
            @ApiParam(name = "id", value = "每条答题记录id", required = true) @PathVariable String id,
            @ApiParam(name = "score", value = "每条答题的成绩", example = "10.1", required = true) @PathVariable Double score) {
        judgeTaskService.updateManualJudge(id, score);
        return ItooResult.build(ItooResult.SUCCESS, "更新成功");
    }

    /**
     * 根据考试Id、题型Id、班级Id汇总每个学生的题型分数
     *
     * @param examId         考试Id
     * @param questionTypeId 题型Id
     * @param examClassId    班级Id
     * @return
     * @author 侯旭日
     * @since 2.0.0 2018-11-23 19:48:2
     */
    @ApiOperation(value = "根据考试Id、题型Id、班级Id汇总每个学生的题型分数", notes = "根据考试Id、题型Id、班级Id汇总每个学生的题型分数")
    @GetMapping(value = "queryEachStuScore/{examId}")
    public ItooResult queryEachStuScore(
            @ApiParam(name = "examId", value = "考试Id", required = true) @PathVariable String examId,
            @RequestParam(value = "questionTypeId", required = false, defaultValue = "") String questionTypeId,
            @RequestParam(value = "examClassId", required = false, defaultValue = "") String examClassId) {
//        judgeTaskService.queryEachStuScore(examId, questionTypeId, examClassId);
        TotalScore totalScore=new TotalScore();
        totalScore.setExamtionId(examId);
        totalScore.setQuestionTypeId(questionTypeId);
        totalScore.setExamClassId(examClassId);
        totalScore.setJudgeTaskService(judgeTaskService);
        Thread thread=new Thread(totalScore);
        thread.start();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功");
    }

    /**
     * 查询 已考完、手动判分、未判分的考试
     *
     * @return
     * @author 刘梦娇
     * @since 2.0.0 2018-12-12 15:46:56
     */
    @ApiOperation(value = "得到考试名称", notes = "得到考试名称")
    @GetMapping(value = "queryExamName")
    public ItooResult queryExamName() {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskService.queryExamName());
    }

    /**
     * 根据考试Id 的到该场考试下手动判分的题型
     *
     * @param examinationId 考试Id
     * @return
     * @author 刘梦娇
     * @since 2.0.0 2018-12-12 15:46:56
     */
    @ApiOperation(value = "得到题型名称", notes = "得到题型名称")
    @GetMapping(value = "queryQuestionTypeName/{examinationId}")
    public ItooResult queryQuestionTypeName(
            @ApiParam(name = "examinationId", value = "考试Id", required = true) @PathVariable String examinationId) {

        List<JudgeTaskModelResult> judgeTaskModelResult = judgeTaskService.queryQuestionTypeName(examinationId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskModelResult);
    }


    /**
     * 根据批卷班级名称模糊查询judgeTask
     *
     * @param teachclassName 批卷班级名称
     * @param operator       操作人
     * @param pageNo         页码
     * @param pageSize       页数
     * @return
     * @author 刘梦娇
     * @since 2.0.0 2018年12月7日21:40:10
     */
    @ApiOperation(value = "根据批卷班级名称模糊查询judgeTask信息")
    @GetMapping(value = "queryLikeClassName/{pageNo}/{pageSize}")
    public ItooResult queryLikeClassName(@RequestParam(required = false, defaultValue = "") String teachclassName,
                                         @RequestParam(required = false, defaultValue = "") String operator,
                                         @ApiParam(value = "页码", example = "1", required = true) @PathVariable Integer pageNo,
                                         @ApiParam(value = "记录数", example = "10", required = true) @PathVariable Integer pageSize) {
        PageInfo<JudgeTaskModelResult> judgeTaskModelResultPageInfo = judgeTaskService.queryLikeClassName(teachclassName, operator, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskModelResultPageInfo);
    }


    /**
     * 修改手动判分任务的批卷人
     *
     * @param judgeTaskModel asd
     * @return 修改后的结果
     * @author 刘梦娇
     * @since 2.0.0 2018-12-14 15:01:12
     */
    @ApiOperation(value = "根据id编辑judgeTask的批卷人")
    @PostMapping(value = "/modifyJudgeTask")
    public ItooResult modifyJudgeTask(
            @ApiParam(name = "Id", value = "每条手动判分任务id", required = true) @RequestBody JudgeTaskModel judgeTaskModel) {
        if (judgeTaskService.modifyJudgeTask(judgeTaskModel)) {
            return ItooResult.build(ItooResult.SUCCESS, "修改成功！");
        } else {
            return ItooResult.build(ItooResult.FAIL, "修改失败！");

        }
    }

    /**
     * 添加判分任务到JudgeTask
     *
     * @param judgeTaskModel JudgeTaskModel
     * @return 添加判分任务
     * @author 杨海云
     * @since 2.0.0 2018-12-12 21:33:10
     */
    @ApiOperation(value = "添加判分任务")
    @PostMapping(value = "/insertJudgeTask")
    public ItooResult insertJudgeTask(@RequestBody JudgeTaskModel judgeTaskModel) {
        boolean is_judgetask = judgeTaskService.insertJudgeTask(judgeTaskModel);
        if (!is_judgetask) {
            return ItooResult.build(ItooResult.FAIL, "该判分任务已添加", false);
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "添加成功", true);
        }

    }


    /**
     * 组合查询，根据考试id，题型id，批卷班级id，批卷教师id
     *
     * @param operator          操作人
     * @param examinationId     考试id
     * @param templateDetailsId 题型id
     * @param teachclassId      批卷班级id
     * @param teacherId         批卷教师id
     * @param pageNo            分页页数
     * @param pageSize          分页大小
     * @return 符合条件的判分任务
     * @author 刘梦娇
     * @since 2018-12-18 19:45:21
     */
    @ApiOperation(value = "组合查询，根据考试id，题型id，批卷班级id，批卷教师id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operator", value = "用户Id"),
            @ApiImplicitParam(name = "examinationId", value = "考试id"),
            @ApiImplicitParam(name = "templateDetailsId", value = "题型id"),
            @ApiImplicitParam(name = "teachclassId", value = "批卷班级id"),
            @ApiImplicitParam(name = "teacherId", value = "批卷教师id"),
            @ApiImplicitParam(name = "pageNo", value = "当前页数",example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小",example = "10")
    })

    @GetMapping(value = "/queryCombine")
    public ItooResult queryCombine(@RequestParam(value = "operator") String operator,
                                   @RequestParam(value = "examinationId", required = false) String examinationId,
                                   @RequestParam(value = "templateDetailsId", required = false) String templateDetailsId,
                                   @RequestParam(value = "teachclassId", required = false) String teachclassId,
                                   @RequestParam(value = "teacherId", required = false) String teacherId,
                                   @RequestParam(value = "pageNo") Integer pageNo,
                                   @RequestParam(name = "pageSize") Integer pageSize) {

        return ItooResult.build(ItooResult.SUCCESS, "查询成功", judgeTaskService.queryCombine(operator, examinationId, templateDetailsId, teachclassId, teacherId, pageNo, pageSize));

    }

    /**
     * 更新考试状态
     * @param examtionId
     * @author 侯旭日
     * @return
     */
    @PutMapping("/updateExamtionState")
    public ItooResult updateExamtionState(@RequestParam(value = "examtionId") String examtionId) {
        judgeTaskService.updateExamtionState(examtionId);
        return ItooResult.build(ItooResult.SUCCESS, "更新成功");
    }

    /**
     * 导入Json
     * @param file
     * @author 侯旭日
     * @throws IOException
     */
    @PostMapping("/importJson")
    @ApiOperation(value = "导入Json")
    public ItooResult importJson(@RequestParam("file")File file) throws IOException {
        //@RequestParam("multipartFile") MultipartFile multipartFile,
//        String[] list = file.list();
//        File f=new File(file.getPath()+"\\"+list[0]);
//        FileInputStream fileInputStream = new FileInputStream(f);
//
//        int available = fileInputStream.available();
//        byte[] bbb = new byte[available];
//        fileInputStream.read(bbb);
//        String aaa=new String(bbb,"UTF-8");
        //                multipartFile.
        boolean flag=judgeTaskService.importJson(file);
        if (flag){
            return ItooResult.build(ItooResult.SUCCESS, "导入成功");
        }else {
            return ItooResult.build(ItooResult.FAIL, "导入失败");
        }
    }

    /**
     *
     * @author 侯旭日
     * @param examtionId
     */
    @ApiOperation(value = "试题已经修改但是把修改之前题抽出来进行判分")
    @GetMapping("/againJudge/{examtionId}")
    public ItooResult againJudge(@PathVariable String examtionId){
        boolean flag=judgeTaskService.againJudge(examtionId);
        if (flag){
            return ItooResult.build(ItooResult.SUCCESS, "成功");
        }else {
            return ItooResult.build(ItooResult.FAIL, "失败");
        }

    }

    @ApiOperation(value = "试题已经删除，按删除的进行判分")
    @GetMapping("/againJudgeQuesIsDelete/{examtionId}")
    public ItooResult againJudgeQuesIsDelete(@PathVariable String examtionId){
        boolean flag=judgeTaskService.againJudgeQuesIsDelete(examtionId);
        if (flag){
            return ItooResult.build(ItooResult.SUCCESS, "成功");
        }else {
            return ItooResult.build(ItooResult.FAIL, "失败");
        }

    }

    @ApiOperation(value = "取消学生答案<p></p>标签")
    @GetMapping("/againJudgeQuesIsDelete")
    public ItooResult againJudgeQuesIsDelete(){
        boolean flag=judgeTaskService.againJudgeQuesIsDelete();
        if (flag){
            return ItooResult.build(ItooResult.SUCCESS, "成功");
        }else {
            return ItooResult.build(ItooResult.FAIL, "失败");
        }
    }

    @ApiOperation(value = "业务题模板配置错误，模板修改正确后重新判分")
    @GetMapping("/againJudgeByBusiness")
    public ItooResult againJudgeByBusiness(){
        boolean flag=judgeTaskService.againJudgeByBusiness();
        if (flag){
            return ItooResult.build(ItooResult.SUCCESS, "成功");
        }else {
            return ItooResult.build(ItooResult.FAIL, "失败");
        }
    }

    @PostMapping("/totalScoreManyThread")
    public ItooResult totalScoreManyThread( @RequestBody List<TotalScoreModel> examineeModelList ) {
        boolean flag=judgeTaskService.totalScoreManyThread(examineeModelList);
        if (flag){
            return ItooResult.build(ItooResult.SUCCESS, "成功");
        }else {
            return ItooResult.build(ItooResult.FAIL, "失败");
        }
    }

}    
